package ioStream.file.nodeStream.byteStream;

import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:字节输出流学习
 * User:22954
 * Date:2024-03-15
 * Time:15:15
 */
@SuppressWarnings("all")
public class FileOutputStream_ {
    /*
    FileOutputStream extends OutputStream
    FileOutputStream(String name) throws FileNotFoundException
        创建一个文件输出流以写入到具有指定名称的文件中----若文件不存在,则自动创建该路径名称的文件  append默认为false
    FileOutputStream(String name, boolean append) throws FileNotFoundException
        如果append为true，那么字节将被写入文件的末尾，而不是开头
    void write(int b) throws IOException  将指定的字节写入此文件输出流
    void write(byte[] b) throws IOException  将指定字节数组中的b.length字节写入此文件输出流
    void write(byte[] b, int off, int len) throws IOException  从偏移量为off的指定字节数组中写入len个字节到此文件输出流
     */
    @Test
    public void writeFile1(){
        String filePath = "f:/Test/hello.txt";
        FileOutputStream fileOutputStream = null;
        try {
//            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream = new FileOutputStream(filePath,true);//若append为false,每次调用write()都会覆盖原文件内容
//            fileOutputStream.write('A');

            String data = "Hello World!";
            fileOutputStream.write(data.getBytes());
            fileOutputStream.write(data.getBytes(),0,5);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
